package com.flyou.ccnucenter.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.view.jazzyviewpager.JazzyViewPager;
import com.flyou.ccnucenter.view.jazzyviewpager.OutlineContainer;

import java.io.InputStream;

/**
 * Created by Administrator on 2015/9/3.
 */
public class WelcomePagerAdapter extends PagerAdapter {
    private JazzyViewPager mViewPager;
    private Context mContext;
    public static  int[] guideId = {R.drawable.guide1, R.drawable.guide2,
            R.drawable.guide3, R.drawable.guide4};

    public WelcomePagerAdapter(JazzyViewPager viewPager, Context activity) {
        mViewPager = viewPager;
        mContext = activity;
    }

    @Override
    public int getCount() {
        return guideId.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        if (view instanceof OutlineContainer) {
            return ((OutlineContainer) view).getChildAt(0) == object;
        } else {
            return view == object;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView(mViewPager.findViewFromObject(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is =mContext.getResources().openRawResource(guideId[position]);
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
        imageView.setImageBitmap(bitmap);
        container.addView(imageView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        //这句话必须要写，按不同的位置添加视图
        mViewPager.setObjectForPosition(imageView, position);
        return imageView;

    }
}
